/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mulanweb.validator;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericTypeValidator;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.util.ValidatorUtils;

import java.util.Date;

/**
 * Contains validation methods for different unit tests.
 *
 * @version $Revision: 1649191 $
 */
public class BaseTypeValidator {


    public static boolean validateBoolean(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }

        if (value.equalsIgnoreCase("true") ||
                value.equalsIgnoreCase("false") ||
                value.equalsIgnoreCase("0") ||
                value.equalsIgnoreCase("1")) {
            return Boolean.TRUE;
        }

        return Boolean.FALSE;
    }

    public static boolean validateByte(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        return null != GenericTypeValidator.formatByte(value);
    }


    public static boolean validateShort(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        return null != GenericTypeValidator.formatShort(value);
    }


    public static boolean validateInt(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        return null != GenericTypeValidator.formatInt(value);
    }


    public static boolean validateLong(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        return null != GenericTypeValidator.formatLong(value);
    }


    public static boolean validateFloat(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        return null != GenericTypeValidator.formatFloat(value);
    }


    public static boolean validateDouble(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        return null != GenericTypeValidator.formatDouble(value);
    }


    public static boolean validateDate(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        if (StringUtils.isBlank(value)) {
            return Boolean.TRUE;
        }
        String datePattern = field.getVarValue("datePattern");
        String datePatternStrict = field.getVarValue("datePatternStrict");

        Date result = null;
        if (datePattern != null && datePattern.length() > 0) {
            result = GenericTypeValidator.formatDate(value, datePattern, false);
        } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
            result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
        }

        return null != result;
    }

    /**
     * Checks if the field is required.
     *
     * @return boolean If the field isn't <code>null</code> and
     * has a length greater than zero, <code>true</code> is returned.
     * Otherwise <code>false</code>.
     */
    public static boolean validateRequired(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        return !GenericValidator.isBlankOrNull(value);
    }
}                                                         